package com.wandian.view.components.ui
{
	import com.wandian.util.ButtonUtil;	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.getTimer;
	import flash.display.SimpleButton;

	/*
	* @author long
	* @date 2012-10-6 下午11:35:22
	* @qq 108232706
	* @description ScrollBarTxt
	*/
	public class ScrollBarTxt extends Sprite
	{
		private var scrollText:TextField;
		private var scrollBar_sprite:Sprite;
		private var up_btn:SimpleButton;
		private var down_btn:SimpleButton;
		private var pole_sprite:MovieClip;
		private var bg_sprite:SimpleButton;
		private var poleStartHeight:Number;
		private var poleStartY:Number;
		private var totalPixels:Number;
		private var isSelect:Boolean;
		private var putTime:Number;
		
		public function ScrollBarTxt(_scrollText:TextField, _scrollBar_sprite:Sprite, bgScrollHeight:uint = 0, bgScrollWidth:uint = 0)
		{
			this.scrollText = _scrollText;
			this.scrollBar_sprite = _scrollBar_sprite;
			this.up_btn = SimpleButton(this.scrollBar_sprite.getChildByName("up_btn"));
			this.down_btn = SimpleButton(this.scrollBar_sprite.getChildByName("down_btn"));
			this.pole_sprite = MovieClip(this.scrollBar_sprite.getChildByName("pole_mc"));
			this.bg_sprite = SimpleButton(this.scrollBar_sprite.getChildByName("bg_mc"));
			ButtonUtil.setButton(this.pole_sprite);
			this.pole_sprite.visible = false;
			this.up_btn.enabled = false;
			this.down_btn.enabled = false;
			this.bg_sprite.useHandCursor = false;
			this.isSelect = this.scrollText.selectable;
			if (bgScrollHeight == 0)
			{
				this.bg_sprite.height = this.scrollText.height;
				this.up_btn.y = this.bg_sprite.y;
				this.down_btn.y = this.bg_sprite.y + this.bg_sprite.height - this.down_btn.height;
			}
			else
			{
				this.bg_sprite.height = bgScrollHeight;
			}
			if (bgScrollWidth != 0)
			{
				this.bg_sprite.width = bgScrollWidth + 2;
				this.pole_sprite.width = bgScrollWidth;
				this.down_btn.height = bgScrollWidth;
				this.down_btn.width = bgScrollWidth;
				this.up_btn.height = bgScrollWidth;
				this.up_btn.width = bgScrollWidth;
			}
			this.poleStartHeight = Math.floor(this.down_btn.y - this.up_btn.y - this.up_btn.height);
			var _poleStartY:int = Math.floor(this.up_btn.y + this.up_btn.height + 1);
			this.pole_sprite.y = Math.floor(this.up_btn.y + this.up_btn.height + 1);
			this.poleStartY = _poleStartY;
			if (_scrollBar_sprite.stage)
			{
				this.initListener();
			}
			else
			{
				this.scrollBar_sprite.addEventListener(Event.ADDED_TO_STAGE, this.initListener);
			}
			return;
		}
		
		private function initListener(event:Event = null) : void
		{
			var e:* = event;
			this.scrollBar_sprite.removeEventListener(Event.ADDED_TO_STAGE, this.initListener);
			this.scrollText.addEventListener(Event.SCROLL, this.textScroll);
			this.scrollText.addEventListener(MouseEvent.MOUSE_WHEEL, this.mouseWheel);
			this.up_btn.addEventListener(MouseEvent.MOUSE_DOWN, this.upBtn);
			this.up_btn.stage.addEventListener(MouseEvent.MOUSE_UP, this.upBtnUp);
			this.down_btn.addEventListener(MouseEvent.MOUSE_DOWN, this.downBtn);
			this.down_btn.stage.addEventListener(MouseEvent.MOUSE_UP, this.downBtnUp);
			this.pole_sprite.addEventListener(MouseEvent.MOUSE_DOWN, this.poleSprite);
			try
			{
				this.pole_sprite.stage.addEventListener(MouseEvent.MOUSE_UP, this.poleUp);
			}
			catch (e:Error)
			{
			}
			this.bg_sprite.addEventListener(MouseEvent.MOUSE_DOWN, this.bgDown);
			return;
		}
		
		public function reScrollBar(param1:TextField, param2:Sprite, param3:uint = 0, param4:uint = 0) : void
		{
			return;
		}
		
		private function textScroll(event:Event = null) : void
		{
			var _loc_2:Number = NaN;
			if (this.scrollText.maxScrollV != 1)
			{
				this.pole_sprite.visible = true;
				this.up_btn.enabled = true;
				this.down_btn.enabled = true;
				_loc_2 = 1 - (this.scrollText.maxScrollV - 1) / this.scrollText.maxScrollV;
				this.pole_sprite.height = Math.floor(this.poleStartHeight * Math.pow(_loc_2, 1 / 3));
				this.totalPixels = Math.floor(this.down_btn.y - this.up_btn.y - this.up_btn.height - this.pole_sprite.height);
				this.pole_sprite.y = Math.floor(this.poleStartY + this.totalPixels * (this.scrollText.scrollV - 1) / (this.scrollText.maxScrollV - 1));
			}
			else
			{
				this.pole_sprite.visible = false;
				this.up_btn.enabled = false;
				this.down_btn.enabled = false;
			}
			return;
		}
		
		private function poleSprite(event:MouseEvent) : void
		{
			var event:* = event;
			this.scrollText.removeEventListener(Event.SCROLL, this.textScroll);
			try
			{
				this.scrollBar_sprite.stage.addEventListener(MouseEvent.MOUSE_UP, this.poleUp);
			}
			catch (e:Error)
			{
			}
			var dragRect:* = new Rectangle(this.pole_sprite.x, this.poleStartY, 0, this.totalPixels);
			this.pole_sprite.startDrag(false, dragRect);
			this.scrollBar_sprite.addEventListener(Event.ENTER_FRAME, this.poleDown);
			return;
		}
		
		private function poleDown(event:Event) : void
		{
			var _loc_2:* = Math.floor(this.pole_sprite.y);
			this.scrollText.scrollV = (this.scrollText.maxScrollV - 1) * (_loc_2 - this.poleStartY) / this.totalPixels + 2;
			var _loc_3:* = this.totalPixels / (this.scrollText.maxScrollV - 1);
			if (_loc_2 - this.poleStartY < _loc_3)
			{
				this.scrollText.scrollV = (this.scrollText.maxScrollV - 1) * (_loc_2 - this.poleStartY) / this.totalPixels;
			}
			return;
		}
		
		private function poleUp(event:MouseEvent) : void
		{
			var event:* = event;
			this.pole_sprite.stopDrag();
			this.scrollBar_sprite.removeEventListener(Event.ENTER_FRAME, this.poleDown);
			try
			{
				if (this.scrollBar_sprite.stage != null)
				{
					this.scrollBar_sprite.stage.removeEventListener(MouseEvent.MOUSE_UP, this.poleUp);
				}
			}
			catch (e:Error)
			{
			}
			this.scrollText.addEventListener(Event.SCROLL, this.textScroll);
			return;
		}
		
		private function bgDown(event:MouseEvent) : void
		{
			var _loc_2:Number = NaN;
			if (this.scrollBar_sprite.mouseY - this.up_btn.y < this.pole_sprite.height / 2)
			{
				_loc_2 = Math.floor(this.up_btn.y + this.up_btn.height);
			}
			else if (this.down_btn.y - this.scrollBar_sprite.mouseY < this.pole_sprite.height / 2)
			{
				_loc_2 = Math.floor(this.down_btn.y - this.pole_sprite.height);
			}
			else
			{
				_loc_2 = this.scrollBar_sprite.mouseY - this.pole_sprite.height / 2;
			}
			this.pole_sprite.y = _loc_2;
			this.scrollText.scrollV = (this.scrollText.maxScrollV - 1) * (_loc_2 - this.poleStartY) / this.totalPixels + 2;
			var _loc_3:* = this.totalPixels / (this.scrollText.maxScrollV - 1);
			if (_loc_2 - this.poleStartY < _loc_3)
			{
				this.scrollText.scrollV = (this.scrollText.maxScrollV - 1) * (_loc_2 - this.poleStartY) / this.totalPixels + 1;
			}
			return;
		}
		
		private function downBtn(event:MouseEvent) : void
		{
			var _loc_2:* = this.scrollText;
			var _loc_3:* = this.scrollText.scrollV + 1;
			_loc_2.scrollV = _loc_3;
			this.pole_sprite.y = Math.floor(this.poleStartY + this.totalPixels * (this.scrollText.scrollV - 1) / (this.scrollText.maxScrollV - 1));
			this.putTime = getTimer();
			this.scrollBar_sprite.addEventListener(Event.ENTER_FRAME, this.downBtnDown);
			return;
		}
		
		private function downBtnDown(event:Event) : void
		{
			if (getTimer() - this.putTime > 500)
			{
				var _loc_2:* = this.scrollText;
				var _loc_3:* = this.scrollText.scrollV + 1;
				_loc_2.scrollV = _loc_3;
				this.pole_sprite.y = Math.floor(this.poleStartY + this.totalPixels * (this.scrollText.scrollV - 1) / (this.scrollText.maxScrollV - 1));
			}
			return;
		}
		
		private function downBtnUp(event:MouseEvent) : void
		{
			this.scrollBar_sprite.removeEventListener(Event.ENTER_FRAME, this.downBtnDown);
			return;
		}
		
		private function upBtn(event:MouseEvent) : void
		{
			var _loc_2:* = this.scrollText;
			var _loc_3:* = this.scrollText.scrollV - 1;
			_loc_2.scrollV = _loc_3;
			this.pole_sprite.y = Math.floor(this.poleStartY + this.totalPixels * (this.scrollText.scrollV - 1) / (this.scrollText.maxScrollV - 1));
			this.putTime = getTimer();
			this.scrollBar_sprite.addEventListener(Event.ENTER_FRAME, this.upBtnDown);
			return;
		}
		
		private function upBtnDown(event:Event) : void
		{
			if (getTimer() - this.putTime > 500)
			{
				var _loc_2:* = this.scrollText;
				var _loc_3:* = this.scrollText.scrollV - 1;
				_loc_2.scrollV = _loc_3;
				this.pole_sprite.y = Math.floor(this.poleStartY + this.totalPixels * (this.scrollText.scrollV - 1) / (this.scrollText.maxScrollV - 1));
			}
			return;
		}
		
		private function upBtnUp(event:MouseEvent) : void
		{
			this.scrollBar_sprite.removeEventListener(Event.ENTER_FRAME, this.upBtnDown);
			return;
		}
		
		private function mouseWheel(event:MouseEvent) : void
		{
			if (this.isSelect == false)
			{
				this.scrollText.scrollV = this.scrollText.scrollV - Math.floor(event.delta / 2);
			}
			else if (this.isSelect == true)
			{
				event.delta = 1;
			}
			this.pole_sprite.y = Math.floor(this.poleStartY + this.totalPixels * (this.scrollText.scrollV - 1) / (this.scrollText.maxScrollV - 1));
			return;
		}
		
		public function disuse() : void
		{
			if (this.scrollText != null)
			{
				this.scrollText.removeEventListener(Event.SCROLL, this.textScroll);
				this.scrollText.removeEventListener(MouseEvent.MOUSE_WHEEL, this.mouseWheel);
			}
			if (this.up_btn != null)
			{
				this.up_btn.removeEventListener(MouseEvent.MOUSE_DOWN, this.upBtn);
				this.up_btn.stage.removeEventListener(MouseEvent.MOUSE_UP, this.upBtnUp);
			}
			if (this.down_btn != null)
			{
				this.down_btn.removeEventListener(MouseEvent.MOUSE_DOWN, this.downBtn);
				this.down_btn.stage.removeEventListener(MouseEvent.MOUSE_UP, this.downBtnUp);
			}
			if (this.pole_sprite != null)
			{
				this.pole_sprite.removeEventListener(MouseEvent.MOUSE_DOWN, this.poleSprite);
				this.pole_sprite.stage.removeEventListener(MouseEvent.MOUSE_UP, this.poleUp);
			}
			if (this.bg_sprite != null)
			{
				this.bg_sprite.removeEventListener(MouseEvent.MOUSE_DOWN, this.bgDown);
			}
			if (this.scrollBar_sprite != null)
			{
				this.scrollBar_sprite.stage.removeEventListener(MouseEvent.MOUSE_UP, this.poleUp);
				this.scrollBar_sprite.removeEventListener(Event.ENTER_FRAME, this.poleDown);
				this.scrollBar_sprite.removeEventListener(Event.ENTER_FRAME, this.downBtnDown);
				this.scrollBar_sprite.removeEventListener(Event.ENTER_FRAME, this.upBtnDown);
			}
			return;
		}
		
	}
}
